<template>
  <div class="main-app">
    <el-form ref="form" :model="form" :rules="rules" label-width="600px" style="margin-top: 50px;">
      <el-form-item label="废物类别:" style="width: 60%;">
        <el-input v-model="form.category" disabled />
      </el-form-item>
      <el-form-item label="废物代码:" prop="number" style="width: 60%;">
        <el-input v-model="form.number" :disabled="isEdit"/>
      </el-form-item>
      <!-- <el-form-item label="危险特性:" prop="characteristics" style="width: 60%;">
        <el-select
          v-model="form.characteristics"
          placeholder="请选择危险特性"
          clearable
          style="width: 100%;"
        >
          <el-option
            v-for="item in wasteCharacteristicsOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item> -->

      <el-form-item label="危险特性" style="width: 60%;" prop="characteristics">
        <template>
          <el-select v-model="form.characteristics" multiple placeholder="请选择危险特性"
                     style="width: 100%;"
          >
            <el-option v-for="item in wasteCharacteristicsOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </template>
      </el-form-item>

      <el-form-item label="状态:" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio
            v-for="(item, index) in options"
            :key="index"
            :label="item.value"
          >{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="描述:" prop="desc" style="width: 60%;">
        <el-input v-model="form.desc" type="textarea" rows="5" clearable />
      </el-form-item>
    </el-form>
    <el-row style="margin-top: 40px; text-align: center">
      <el-form :inline="true" class="form-inline">
        <el-form-item>
          <el-button type="primary" @click="save">提交</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-row>
  </div>
</template>

<script>
import {
  EquipmentStatus,
  WasteCharacteristics,
} from "../../../../utils/resource";
import { save, getById } from "../../../../api/system";
export default {
  data() {
    return {
      form: {
        code: "",
        status: "0",
        number: "",
        characteristics: "",
        desc: "",
        category: "",
      },
       isEdit:false,
      options: EquipmentStatus,
      wasteCharacteristicsOptions: WasteCharacteristics,
      rules: {
        number: [
          { required: true, message: "请输入废物代码", trigger: "blur" },
        ],
        characteristics: [
          { required: true, message: "请选择危险特性", trigger: "change" },
        ],
        category: [
          { required: true, message: "请输入类别标号", trigger: "blur" },
        ],
        desc: [{ required: true, message: "请输入描述", trigger: "blur" }],
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
      },
      id: "",
      code: "",
    };
  },
  async created() {
    const { id, code,type } = this.$route.params;
    this.id = id;
    this.form.category = this.id;
    this.code = code;
	await this.getById(code,type);
  if(this.code){
    this.isEdit=true;
  }else{
     this.isEdit=false;
  }
  },
  methods: {
	   async getById(id,type) {
	     const { data } = await getById({ type: type, code: id });
      this.form.number = data.code;
      this.form.characteristics = data.value ? data.value.split(',') : "";
      this.form.category = this.id;
      this.form.status = data.status.toString();
      this.form.desc = data.customOne;
    },
    save() {
      this.$refs["form"].validate(async(valid) => {
        if (valid) {
          var characteristics = "";
          if (this.form.characteristics) {
            for (let j = 0; j < this.form.characteristics.length; j++) {
              if (j === 0) {
                characteristics += this.form.characteristics[j];
              } else {
                characteristics += ("," + this.form.characteristics[j])
              }
            }
          }

          let data = {
            type: "105",
            value: characteristics,
            status: this.form.status,
            code: this.form.number,
            sort: Number(this.form.category),
            customOne: this.form.desc,
            parentId: this.id,
            customThree: this.code,
          };
          // return
          let message = "";
          if (!this.code) {
			   data = Object.assign({ isAdd: "1" }, data);
            message = "废物代码新增成功";
          } else {
            data = Object.assign({ code: this.code }, data);
            message = "废物代码修改成功";
          }
          const { success, msg } = await save(data);
          if (success) {
            this.$notify({
              title: "成功",
              message: message,
              type: "success",
              duration: 3000,
            });
            this.$router.push(`/system/type/code/${this.id}`);
          } else {
            this.$notify.error({
              title: "失败",
              message: msg,
              duration: 3000,
            });
          }
        }
      });
    },
    cancel() {
      this.$router.push(`/system/type/code/${this.id}`);
    },
  },
};
</script>
